<script setup lang="ts">
import { ElMessage, type FormInstance } from 'element-plus'
import { ref } from 'vue'
import type { GroupTable } from '@/types'
import { formRules } from './methodType'
import { updateGroup } from './api'
const props = defineProps<{
  dMap: Map<string, string>
  editData: GroupTable
  // 刷新
  refresh: () => void
}>()
// 关闭
const emit = defineEmits<{
  (event: 'cDlg'): void
}>()

// 表单dom
const elFormRef = ref<FormInstance>()
function edit() {
  elFormRef.value?.validate((d) => {
    if (d) {
      updateGroup(props.editData)
        .then((d) => {
          ElMessage.success(d.msg)
          props.refresh()
          emit('cDlg')
        })
        .catch((d) => {
          ElMessage.error(d.msg)
        })
    }
  })
}
function cancel() {
  emit('cDlg')
  props.refresh()
}
</script>

<template>
  <el-form ref="elFormRef" :model="editData" :rules="formRules" label-width="150px">
    <el-form-item required :label="(dMap.get('name') || 'name') + '：'" prop="name">
      <el-input
        v-model="editData.name"
        :placeholder="'请输入' + dMap.get('name') || 'name'"
      ></el-input>
    </el-form-item>
    <el-form-item
      required
      :label="(dMap.get('description') || 'description') + '：'"
      prop="description"
    >
      <el-input
        v-model="editData.description"
        :placeholder="'请输入' + dMap.get('description') || 'description'"
      ></el-input>
    </el-form-item>
    <el-form-item required :label="(dMap.get('status') || 'status') + '：'" prop="status">
      <el-switch
        v-model="editData.status"
        style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
        active-value="1"
        inactive-value="0"
      ></el-switch>
    </el-form-item>
    <div class="buttons">
      <el-button type="primary" @click="cancel">取消</el-button>
      <el-button type="success" @click="edit">保存</el-button>
    </div>
  </el-form>
</template>
<style scoped lang="less">
.buttons {
  display: flex;
  margin-top: 30px;
  justify-content: flex-end;
}
</style>
